package kxfang.com.find.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.orhanobut.hawk.Hawk;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import kxfang.com.base.activity.MvvmBaseActivity;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.constant.Constant;
import kxfang.com.common.parameter.CommonPar;
import kxfang.com.common.router.RoutHub;
import kxfang.com.common.utils.GetTimeAgo;
import kxfang.com.common.utils.HawkUtil;
import kxfang.com.common.utils.MyUtils;
import kxfang.com.common.utils.RouterUtils;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.common.utils.TimeUtil;
import kxfang.com.find.R;
import kxfang.com.find.bean.FindListModel;
import kxfang.com.find.databinding.FindActivityItFxBinding;
import kxfang.com.find.databinding.FindItemPersonalInformation2Binding;
import kxfang.com.find.databinding.FindItemPersonalInformationBinding;
import kxfang.com.find.databinding.FindRvImgItemBinding;
import kxfang.com.find.databinding.FindRvLbItemBinding;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;

import static kxfang.com.common.router.RoutHub.IT_FIND;
import static kxfang.com.common.router.RoutHub.WEB_MAIN;

@Route(path = IT_FIND)
public class ItFxActivity extends MvvmBaseActivity<FindActivityItFxBinding, IMvvmBaseViewModel> {


    private BaseQuickAdapter<String, BaseViewHolder>adapterLb;
    private BaseQuickAdapter<FindListModel.ImgsBean, BaseViewHolder> adapterImg;
    private BaseQuickAdapter<FindListModel, BaseViewHolder> adapter;

    List<String> lbList =new ArrayList<>();
    List<FindListModel.ImgsBean> imgList =new ArrayList<>();
    List<FindListModel> list =new ArrayList<>();

    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTopActivityView(this, viewDataBinding.topView);
        MyUtils.showLoading(this);
        viewDataBinding.myCardBack.setOnClickListener(view -> finish());

        initView();
        getData(true);
        viewDataBinding.srlRelease.setOnRefreshListener(refreshLayout -> getData(true));
        viewDataBinding.srlRelease.setOnLoadMoreListener(refreshLayout -> getData(false));
    }

    private void getData(boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        }
        Intent intent = getIntent();
        CommonPar par = new CommonPar();
        par.setRUserID(intent.getStringExtra("userid"));
        par.setPageSize(10);
        par.setPageIndex(page);
        EasyHttp.post(HttpConfig.gethislist)
                .cacheKey(HttpConfig.gethislist)
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<List<FindListModel>>() {
                    @Override
                    public void onSuccess(List<FindListModel> findListModel) {
                        if (findListModel != null && findListModel.size() != 0) {
                            viewDataBinding.tvName.setText(findListModel.get(0).getNickName());
                            Glide.with(ItFxActivity.this)
                                    .load(Constant.PHOTO_SERVER_URL + findListModel.get(0).getHeadUrl())
                                    .error(R.drawable.default_head)
                                    .circleCrop()
                                    .into(viewDataBinding.ivHeadTa);
                        }

                        if (isRefresh) {
                            if (findListModel != null && findListModel.size() != 0) {
                                viewDataBinding.icNoData.setVisibility(View.GONE);
                                list.clear();
                                list.addAll(findListModel);
                                if (findListModel.size() < 10) {
                                    viewDataBinding.srlRelease.setNoMoreData(true);
                                }
                                viewDataBinding.srlRelease.finishRefresh();
                            } else {
                                viewDataBinding.srlRelease.setNoMoreData(true);
                                viewDataBinding.icNoData.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (findListModel != null && findListModel.size() != 0) {
                                list.addAll(findListModel);
                                if (findListModel.size() < 10) {
                                    viewDataBinding.srlRelease.finishLoadMoreWithNoMoreData();
                                } else {
                                    viewDataBinding.srlRelease.finishLoadMore();
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onCompleted() {
                        MyUtils.disLoading();
                    }
                });

    }

    private void initView() {
        adapter =new BaseQuickAdapter<FindListModel, BaseViewHolder>(R.layout.find_item_personal_information2, list) {
            @Override
            protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                DataBindingUtil.bind(viewHolder.itemView);
            }
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable FindListModel findListModel) {
                FindItemPersonalInformation2Binding binding = baseViewHolder.getBinding();

                binding.tvShow.setVisibility(View.GONE);
                binding.tvDz.setVisibility(View.GONE);
                binding.ivShare.setVisibility(View.GONE);
                binding.llPhone.setVisibility(View.GONE);

                //参数列表
                binding.rvLb.setLayoutManager(new LinearLayoutManager(getContext()));
                RvLb(findListModel.getIsDelete());
                binding.rvLb.setAdapter(adapterLb);

                adapterLb.setOnItemClickListener((adapter1, view, position) -> {
                    Bundle bundle = new Bundle();
                    String url = "";
                    if (HawkUtil.getUserId() == null) {
                        url = Constant.LOAD_WEBVIEWURL.concat(
                                String.format("/fabu/fxinfo.html?type=mobile&id=%s&uname=%s&CType=1"
                                        , findListModel.getID(), Hawk.get("uname")));
                    } else {
                        url = Constant.LOAD_WEBVIEWURL.concat(
                                String.format("/fabu/fxinfo.html?type=mobile&userid=%s&id=%s&uname=%s&CType=1"
                                        , String.valueOf(HawkUtil.getUserId()), findListModel.getID(), Hawk.get("uname")));
                    }
                    bundle.putString("url", url);
                    bundle.putString("title", "详情");

                    RouterUtils.navigate(RoutHub.WEB_MAIN, bundle, getContext());
                });

                //图片合集
                binding.rvImg.setLayoutManager(new GridLayoutManager(getContext(), 3));
                RvImg(findListModel.getIsDelete());
                binding.rvImg.setAdapter(adapterImg);

                binding.tvName.setText(findListModel.getNickName());
                binding.tvIntroduction.setText(findListModel.getTitle());
                binding.tvBrowse.setText(findListModel.getViewCount() + "浏览");
                binding.tvShare.setText(findListModel.getShareCount() + "分享");

                binding.tvTime.setText(GetTimeAgo.getTimeAgo(Long.valueOf(TimeUtil.getTimeall(findListModel.getCreateDate())) * 1000));
                Glide.with(getContext())
                        .load(Constant.PHOTO_SERVER_URL + findListModel.getHeadUrl())
                        .error(R.drawable.default_head)
                        .circleCrop()
                        .into(binding.ivImg);

                    lbList.clear();
                if (findListModel.getClassName().equals("二手车")) {
                    lbList.addAll(findListModel.getListOldcar());
                    binding.tvType.setText("二手车");
                } else if (findListModel.getClassName().equals("闲置品")) {

                    lbList.addAll(findListModel.getListXzp());
                    binding.tvType.setText("闲置品");
                } else if (findListModel.getClassName().equals("商铺/写字楼")) {

                    lbList.addAll(findListModel.getListXzl());
                    binding.tvType.setText("商铺/写字楼");
                } else if (findListModel.getClassName().equals("求购需求")) {
                    lbList.addAll(findListModel.getListQgxq());
                    binding.tvType.setText("求购需求");
                }

                if (findListModel.getIsDelete() == 2) {
                    binding.ivCj.setVisibility(View.VISIBLE);
                    binding.tvIntroduction.setTextColor(getResources().getColor(R.color.common_color_A9A9A9));
                } else {
                    binding.ivCj.setVisibility(View.GONE);
                    binding.tvIntroduction.setTextColor(getResources().getColor(R.color.common_color_333333));
                }


                if (findListModel.getImgs() != null && findListModel != null) {
                    imgList.addAll(findListModel.getImgs());
                }
            }
        };
        //跳转详情
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Bundle bundle1 = new Bundle();
            String url = "";

            if (HawkUtil.getUserId() == null) {
                url = Constant.LOAD_WEBVIEWURL.concat(
                        String.format("/fabu/fxinfo.html?type=mobile&id=%s&uname=%s&CType=1"
                                , list.get(position).getID(), Hawk.get("uname")));
            } else {
                url = Constant.LOAD_WEBVIEWURL.concat(
                        String.format("/fabu/fxinfo.html?type=mobile&userid=%s&id=%s&uname=%s&CType=1"
                                , String.valueOf(HawkUtil.getUserId()), list.get(position).getID(), Hawk.get("uname")));
            }
            bundle1.putString("url", url);
            bundle1.putString("title", "详情");
            RouterUtils.navigate(RoutHub.WEB_MAIN, bundle1, this);
        });

        viewDataBinding.recycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        viewDataBinding.recycleView.setAdapter(adapter);
    }

    //参数列表
    private void RvLb(int status) {
        adapterLb = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.find_rv_lb_item, lbList) {
            @Override
            protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                DataBindingUtil.bind(viewHolder.itemView);
            }
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable String s) {
                FindRvLbItemBinding lbBinding = baseViewHolder.getBinding();
                String[] temp = s.split(":");
                if (status == 2) {
                    lbBinding.tvLbQian.setTextColor(getResources().getColor(R.color.common_color_A9A9A9));
                    lbBinding.tvLbHou.setTextColor(getResources().getColor(R.color.common_color_A9A9A9));
                } else {
                    lbBinding.tvLbQian.setTextColor(getResources().getColor(R.color.common_color_2947));
                    lbBinding.tvLbHou.setTextColor(getResources().getColor(R.color.common_color_333333));
                }
                lbBinding.tvLbQian.setText(temp[0].concat("："));
                lbBinding.tvLbHou.setText(temp[1]);
            }
        };
    }

    //图片列表
    private void RvImg(int status) {
        adapterImg = new BaseQuickAdapter<FindListModel.ImgsBean, BaseViewHolder>(R.layout.find_rv_img_item, imgList) {

            @Override
            protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                DataBindingUtil.bind(viewHolder.itemView);
            }

            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, FindListModel.@Nullable ImgsBean imgsBean) {
                FindRvImgItemBinding imgBinding = baseViewHolder.getBinding();
                if (status == 2) {
                    imgBinding.viewMask.setVisibility(View.VISIBLE);
                } else {
                    imgBinding.viewMask.setVisibility(View.GONE);
                }

                Glide.with(getContext())
                        .load(Constant.PHOTO_SERVER_URL + imgsBean.getPicurl())
                        .thumbnail(0.1f)
                        .override(140)
                        .placeholder(R.drawable.house_bitmap)
                        .into(imgBinding.ivImgImg);

            }
        };
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.find_activity_it_fx;
    }


    @Override
    protected void onRetryBtnClick() {

    }
}
